package com.biwano.swapmyjob.beans.user;

import java.io.BufferedReader;

import java.io.InputStreamReader;
import java.net.URL;


import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;

import com.biwano.swapmyjob.dao.JobTypeDao;
import com.biwano.swapmyjob.dao.UserDao;
import com.biwano.swapmyjob.objects.JobType;


@ManagedBean
@RequestScoped
public class DebugBean {
	
	private static final boolean DEBUG_ACTIVATED=true;

	@EJB
	UserDao userDao;
	
	@EJB
	JobTypeDao jobTypeDao;
	
	public String execute(String command)
	{
		String res="deactivated";
		if (DEBUG_ACTIVATED)
		{
			res="i do not understand";
			try
			{
				String [] parts = command.split("[|]");
				if (parts[0].equals("deleteuser"))
				{
					userDao.removeEntity(parts[1]);
					res="Deleted user: " + parts[1];
				}
				if (parts[0].equals("loadjobtypes"))
				{
					jobTypeDao.drop();
					URL url = this.getClass().getResource("/META-INF/jobs.txt");
					BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));
							
					String inputLine;
					res="";
	            	long id =1;
					while ((inputLine = in.readLine()) != null)
					{
						res+=inputLine +"\n";
			            System.out.println(inputLine);
			            if (!inputLine.contains("- see "))
			            {
			            	jobTypeDao.persist(new JobType(inputLine));
			            	id++;
			            }
					}
			        in.close();
				}
			}
			catch (Exception e)
			{
				res = "error: " + e.getMessage();
				e.printStackTrace();
			}
		}
		return res;
	}

}
